import {
  addOrUpdateStudyMoudle,addOrUpdateReportSetting,savePhoto,deletePhoto,updateSort,changeModuleOrder,deleteZituoMethod,deleteZituoSetting,submitZituoMethod,getProjectList,verifyCodeFun
  } from './configurationCenterServ'
  // import { parse } from 'qs'
  import { message } from 'antd'
  // import moment from 'moment'
  import $ from 'jquery'
  import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
  
  const defultState = {
      biz:{
        queryForm: {}
      },
      biz1: {
        queryForm: {}
      },
      biz2: {
        queryForm: {}
      },
      biz3: {
        queryForm: {}
      },
      zituoToken: '',
      tabsType: '',
      areaList: [],
      products: [],
      projectAll: [],
      submitLoading: false,
      backLoading: false,
      id:"",
      customerLabelModal: {
        visible: false
      },
      talkWayModal: {
        visible : false
      },
      graphicModal:{
        visible : false
      },
      labelQuery: { //学堂参数
        id: '',
        moudleName: '',
        operater: localStorage.getItem('username'),
        tableType: ''
      },
      zituoQuery: { //拓客方式
        id: '',
        methodName: '',
      },
      otherQuery:{
        areaId:"",
        projectId:""
      },
      graphicData:{
        id: '',
        type: '2', //1图片2文字
        slideUrl: '',
        text: ''
      }
  }
  
  const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
      setup ({dispatch, history}) {
        history.listen(({ pathname, query }) => {
          if('' + tmpModule.pathname === '' + pathname){
            dispatch({type: 'getProjectList', payload: {}})
            dispatch({type: 'updateStore', payload: cloneDeep(defultState)});
          }
        })
      },
    },
  
    effects: {
      //启用禁用
      *changeStatus({ payload }, { put, call, select }) {
        let params = payload.params
        if(payload.type == '拓客学堂'){
            let { data, success } = yield call(addOrUpdateStudyMoudle, {...params});
            let {biz} = yield select(state => state[tmpModule.namespace])
            if (success) {
                message.success('操作成功');
                biz.refreshListDatas(true)
            }
        }
      },
      *updateSort({ payload }, { put, call, select }) {
          let params = {
            id:payload.params.id
          }
          let {biz1} = yield select(state => state[tmpModule.namespace])
          let index = biz1.tableData.dataSource.findIndex(item => item.id === payload.params.id);
          params.sort = biz1.tableData.dataSource[index-1].sort
          let { data, success } = yield call(updateSort, {...params});
          if (success) {
              message.success('操作成功');
              biz1.refreshListDatas(true)
          }
      },

      *changeModuleOrder({ payload }, { put, call, select }) {
        if(payload.params.orderCol == '1'||payload.params.orderCol == '2'){
          message.warn('最新推荐不能更换');
          return
        }
        let params = [
          {
            id: payload.params.id,
            orderCol: ""
          },
          {
            id: '',
            orderCol: payload.params.orderCol
          },
        ]
        let {biz} = yield select(state => state[tmpModule.namespace])
        let index = biz.tableData.dataSource.findIndex(item => item.id === payload.params.id);
        params[0].orderCol = biz.tableData.dataSource[index-1].orderCol
        params[1].id = biz.tableData.dataSource[index-1].id
        let { data, success } = yield call(changeModuleOrder, params);
          if (success) {
              message.success('操作成功');
              biz.refreshListDatas(true)
          }
      },

      //获取区域项目
      *getProjectList({payload},{put,select,call}){
        let {areaList,projectAll,tabsType} = yield select(state => state[tmpModule.namespace])
        let subElements =  JSON.parse(localStorage.navBar).subElements
        if(subElements.includes(10601)){
          tabsType = '1'
        }
        if(!subElements.includes(10601)&&subElements.includes(10602)){
          tabsType = '2'
        }
        if(!subElements.includes(10601)&&!subElements.includes(10602)&&subElements.includes(10603)){
          tabsType = '3'
        }
        if(!subElements.includes(10601)&&!subElements.includes(10602)&&!subElements.includes(10603)&&subElements.includes(10604)){
          tabsType = '4'
        }
        let params = {
            level : payload.areaCode?4:2,
            companyguid:payload.areaCode?payload.areaCode: ''
          }
          if(yield call(verifyCodeFun,{})){
          const { data } = yield call(getProjectList, {...params})
            let list = data
            if(!list.length){
              return
            }
            areaList =  list.map((item,index)=>{
              return {
                label: item.buname,
                value: item.buguid,
                children: item.childSysUninessunitList
              }
            })
            for(var item of list){
              for(var itemed of item.childSysUninessunitList){
                if(itemed.buname&&item.buguid){
                  projectAll.push(itemed)
                }
              }
            }
            yield put({type: 'updateStore', payload: {areaList,projectAll,tabsType}})
            yield put({type: 'filterProjectList', payload: {}})
          }
          if(subElements.includes(10601)){
            $(".configuration-center-tabs .ant-tabs-tab").eq(0).css('display','inline-block');
          }else{
            $(".configuration-center-tabs .ant-tabs-tab").eq(0).css('display','none');
          }
          if(subElements.includes(10602)){
            $(".configuration-center-tabs .ant-tabs-tab").eq(1).css('display','inline-block');
          }else{
            $(".configuration-center-tabs .ant-tabs-tab").eq(1).css('display','none');
          }
          if(subElements.includes(10603)){
            $(".configuration-center-tabs .ant-tabs-tab").eq(2).css('display','inline-block');
          }else{
            $(".configuration-center-tabs .ant-tabs-tab").eq(2).css('display','none');
          }
          if(subElements.includes(10604)){
            $(".configuration-center-tabs .ant-tabs-tab").eq(3).css('display','inline-block');
          }else{
            $(".configuration-center-tabs .ant-tabs-tab").eq(3).css('display','none');
          }
      },
      *deleteContent({payload},{put,select,call}){
        let params = {id :payload.params.id}
        if(payload.type == '拓客方式'){
          let { success, msg, data } = yield call(deleteZituoMethod, {...params})
          let {biz1} = yield select(state => state[tmpModule.namespace])
          if(success){
            message.success('删除成功')
              biz1.refreshListDatas(true)
          }
        }else if(payload.type == '参数配置'){
          let { success, msg, data } = yield call(deleteZituoSetting, {...params})
          let {biz2} = yield select(state => state[tmpModule.namespace])
          if(success){
            message.success('删除成功')
            biz2.refreshListDatas(true)
          }
        }else if(payload.type == '激励图文'){
          let { success, msg, data } = yield call(deletePhoto, params)
          let {biz3} = yield select(state => state[tmpModule.namespace])
          if(success){
            message.success('删除成功')
            biz3.refreshListDatas(true)
          }
        }else{
        }
      },

      *submit({payload},{put,select,call}){
        let {graphicData,graphicModal,zituoQuery,labelQuery,customerLabelModal,talkWayModal } = yield select(state => state[tmpModule.namespace])
        if(payload.type == '拓客学堂'){
          let params = labelQuery
          const { success, msg, data } = yield call(addOrUpdateStudyMoudle, {...params})
          let {biz} = yield select(state => state[tmpModule.namespace])
  
          if(success){
            customerLabelModal.visible = !customerLabelModal.visible
            yield put({type: 'updateStore', payload: {customerLabelModal}})
            message.success('操作成功')
              biz.refreshListDatas(true)
            yield put({type: 'updateStore', payload: {labelQuery}})
          }
        }else if(payload.type == '拓客方式'){
          let params1 = zituoQuery
          const { success, msg, data } = yield call(submitZituoMethod, {...params1})
          let {biz1} = yield select(state => state[tmpModule.namespace])
  
          if(success){
            talkWayModal.visible = !talkWayModal.visible
            yield put({type: 'updateStore', payload: {talkWayModal}})
            message.success('操作成功')
            biz1.refreshListDatas(true)
            yield put({type: 'updateStore', payload: {zituoQuery}})
          }
        }else if(payload.type == '激励图文'){
          
          let params2 = {
            id:graphicData.id,
            content: graphicData.type=='1'?graphicData.slideUrl:graphicData.text,
            type: Number(graphicData.type)
          }
          const { success, msg, data } = yield call(savePhoto, params2)
          let {biz3} = yield select(state => state[tmpModule.namespace])
          if(success){
            graphicModal.visible = !graphicModal.visible
            message.success('操作成功')
            biz3.refreshListDatas(true)
          }
        }
      },
      *filterProjectList({payload},{put,select,call}){    
        let {products,areaList,biz2} = yield select(state => state[tmpModule.namespace])
        let list = []
        for(var item of areaList){
          if(biz2.queryForm.areaId&&item.value == biz2.queryForm.areaId){
            list = item.children
          }
        }
        if(list.length){
          products =  list.map((item,index)=>{
            return {
              label: item.buname,
              value: item.buguid,
            }
          })
        }
        yield put({type: 'updateStore', payload: {products}})
      },
      
    },
    // 存入状态机
    reducers: {
      updateStore(preState, action) {
        return Object.assign({}, preState, action.payload)
      },
    }
  }
  
  export default tmpModule
  